package com.example.smartcity_poverty.ui.ItemAllService.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.example.smartcity_poverty.App
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.ui.Home.HomeFragment
import com.example.smartcity_poverty.ui.Home.adapter.MoreAdapter
import kotlinx.android.synthetic.main.fragment_all_more.*
import kotlinx.coroutines.Dispatchers

class AllMoreFragment : Fragment() {

    // 存储全部服务数据
    val arrMore = ArrayList<HomeFragment.HomeMoreEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_all_more, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 获取服务
        val moreTypeResult = when (moreTypeId) {
            0 -> "便民服务"
            1 -> "null"
            2 -> "车主服务"
            3 -> "生活服务"
            else -> throw Exception("全部服务模块出错!")
        }
        getService(moreTypeResult)
    }

    /**
     * 根据Type获取指定的服务
     */

    private fun getService(typeName: String) = coroutine(Dispatchers.Main) {
        arrMore.clear()
        // 获取数据
        val moreResult = api.getMore()
        if (moreResult.code == 200) {
            for (i in moreResult.rows!!) {
                if (i.serviceType.toString() == typeName) {
                    if (typeName == "null") {
                        arrMore.add(
                            HomeFragment.HomeMoreEntity(
                                i.sort, App.url + i.imgUrl, 0, i.serviceName.toString()
                            )
                        )
                        arrMore.add(HomeFragment.HomeMoreEntity(0, "", 0, "律师咨询"))
                        arrMore.add(HomeFragment.HomeMoreEntity(0, "", 0, "门诊预约"))
                        arrMore.add(HomeFragment.HomeMoreEntity(0, "", 0, "违章查询"))
                    } else {
                        // 添加数据
                        arrMore.add(
                            HomeFragment.HomeMoreEntity(
                                i.sort, App.url + i.imgUrl, 0, i.serviceName.toString()
                            )
                        )
                    }

                }
                // 设置列表适配器
                setRecyclerAdapter(arrMore)
            }
        }
    }


    /**
     * 列表适配器
     */
    private fun setRecyclerAdapter(data: ArrayList<HomeFragment.HomeMoreEntity>) {
        typeRv.apply {
            this.layoutManager = GridLayoutManager(context, 4)
            this.adapter = MoreAdapter(
                true,
                arrMore.size,
                this@AllMoreFragment,
                data,
                R.layout.re_item_more
            )
            this.adapter?.notifyItemChanged(arrMore.size)
        }
    }


    companion object {
        // 根据Type来获取不同的服务模块
        var moreTypeId = 0
    }
}